{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# Getting started with Quantinuum and OpenQASM 2.0 on Azure Quantum\n",
    "\n",
    "This notebook shows how to send a basic quantum circuit expressed using the [OpenQASM 2.0 spec](https://github.com/Qiskit/openqasm/tree/OpenQASM2.x) to a Quantinuum target via the Azure Quantum service."
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "First, install `azure-quantum` and optionally `matplotlib` for plotting:"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "source": [
    "# To install the Azure Quantum client package, uncomment and run the line below:\n",
    "#\n",
    "# !pip install azure-quantum==0.19.2109.165653 --quiet\n",
    "#\n",
    "# We also recommend installing matplotlib, if you don't have it installed already:\n",
    "# !pip install matplotlib --quiet"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Connecting to the Azure Quantum service\n",
    "\n",
    "To connect to the Azure Quantum service, find the resource ID and location of your Workspace from the Azure portal here: https://portal.azure.com. Navigate to your Azure Quantum workspace and copy the values from the header.\n",
    "\n",
    "<img src=\"../azure-quantum-resource-id.png\" alt=\"How to retrieve the resource ID and location from an Azure Quantum workspace\">"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "source": [
    "from azure.quantum import Workspace\n",
    "from azure.quantum.target import Quantinuum\n",
    "\n",
    "# Enter your workspace details here\n",
    "# Find your resource ID and location via portal.azure.com\n",
    "workspace = Workspace(\n",
    "    resource_id=\"\",\n",
    "    location=\"\"\n",
    ")"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Use `workspace.get_targets` to see what targets are currently available for the Quantinuum provider, including wait times. Running this method will trigger authentication to your Microsoft account, if you're not already logged in."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "source": [
    "workspace.get_targets(provider_id=\"quantinuum\")"
   ],
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "[<Target name=\"quantinuum.qpu.h1-1\", avg. queue time=0 s, Unavailable>,\n",
       " <Target name=\"quantinuum.sim.h1-1sc\", avg. queue time=0 s, Available>,\n",
       " <Target name=\"quantinuum.sim.h1-1e\", avg. queue time=766 s, Available>]"
      ]
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Submit a quantum circuit to the Quantinuum API validator\n",
    "\n",
    "Note: The [Quantinuum API validator](https://docs.microsoft.com/azure/quantum/provider-quantinuum#api-validator) target will always return 0 on measurement.\n",
    "\n",
    "Create a quantum circuit using the [OpenQASM 2.0 spec](https://github.com/Qiskit/openqasm/tree/OpenQASM2.x) representation. For example, the following example creates a Teleportation circuit:"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "# Create raw OpenQASM circuit. \n",
    "circuit = \"\"\"OPENQASM 2.0;\n",
    "include \"qelib1.inc\";\n",
    "qreg q[3];\n",
    "creg c0[1];\n",
    "creg c1[3];\n",
    "h q[0];\n",
    "cx q[0], q[1];\n",
    "x q[2];\n",
    "h q[2];\n",
    "cx q[2], q[0];\n",
    "h q[2];\n",
    "measure q[0] -> c1[0];\n",
    "c0[0] = c1[0];\n",
    "if (c0==1) x q[1];\n",
    "c0[0] = 0;\n",
    "measure q[2] -> c1[1];\n",
    "c0[0] = c1[1];\n",
    "if (c0==1) z q[1];\n",
    "c0[0] = 0;\n",
    "h q[1];\n",
    "measure q[1] -> c1[2];\n",
    "\"\"\""
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "To see if this circuit is valid, we can submit it to the Quantinuum API validator target. The following example uses the Quantinuum API validator, which returns a Job object. For more information, see [Azure Quantum Job](https://review.docs.microsoft.com/azure/quantum/optimization-job-reference)."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "source": [
    "target = workspace.get_targets(name=\"quantinuum.sim.h1-1sc\")\n",
    "job = target.submit(circuit)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Wait until the job is complete and then fetch the results."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "results = job.get_results()\n",
    "results"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "......."
     ]
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "{'c0': ['0'], 'c1': ['000']}"
      ]
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "### Run on Quantinuum Simulator"
   ],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "It looks like the program was indeed valid! Now we can run the circuit and simulate the result with the Quantinuum simulator target:"
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "source": [
    "target = Quantinuum(workspace=workspace, name=\"quantinuum.sim.h1-1e\")\n",
    "job = target.submit(circuit)"
   ],
   "outputs": [],
   "metadata": {}
  },
  {
   "cell_type": "markdown",
   "source": [
    "Wait until the job is complete and then fetch the results."
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "source": [
    "results = job.get_results()\n",
    "results"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "...................\n",
      "{'c0': ['0'], 'c1': ['111']}\n",
      "\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "source": [],
   "outputs": [],
   "metadata": {}
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "506b47c63e104e52a519de0609a284d846e996cd7c1c78a3375aa75d9d74138e"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}